from google.appengine.ext import db
import datetime

class Person(db.Model):

    user = db.UserProperty()
    create_date = db.DateTimeProperty(required=True, auto_now_add=True)
    friendly_name = db.StringProperty()
    gender = db.StringProperty(default="")
    registration_date = db.DateTimeProperty()
    update_count = db.IntegerProperty(default=0)

    def future_updates(self, fetch):
        updates = self.updates.filter("finish_time >", datetime.datetime.now())
        if fetch:
            return updates.fetch(fetch)
        else:
            return updates

    def friends(self):
        friends = []
        for friendship in self.friendships:
            friends.append(friendship.them)
        return friends

    def nickname(self):
        return self.user.nickname()

    def __unicode__(self):
        return self.nickname()

class Verb(db.Model):

    name = db.StringProperty(required=True)
    create_date = db.DateTimeProperty(required=True, auto_now_add=True)
    used_count = db.IntegerProperty(required=True, default=0)

    def __unicode__(self):
        return self.name

class Update(db.Model):

    owner = db.ReferenceProperty(Person, required=True, collection_name='updates')
    verb = db.ReferenceProperty(Verb, required=True)
    body = db.StringProperty()
    start_time = db.DateTimeProperty(required=True, auto_now_add=True)
    finish_time = db.DateTimeProperty()

    def body_fpc(self):
        body = " %s " % self.body
        # Unescape "!" values
        for word in ("my", "me"):
            body = body.replace(" !%s " % word.upper(), " %s " % word.upper()).replace(" !%s " % word.lower(), " %s " % word.lower()).replace(" !%s " % word.title(), " %s " % word.title())
        body = body.strip()
        return body

    def body_tpc(self):
        srdict = {"my":"his/her", "me":"him/her"}
        if self.owner.gender == "m":
            srdict = {"my":"his", "me":"him"}
        if self.owner.gender == "f":
            srdict = {"my":"her", "me":"her"}
        body = " %s " % self.body
        # Obscure escaped values to prevent replacing
        for word in ("my", "me"):
            body = body.replace(" !%s " % word.upper(), " ##%s## " % word.upper()).replace(" !%s " % word.lower(), " ##%s## " % word.lower()).replace(" !%s " % word.title(), " ##%s## " % word.title())
        # Perform actual replacement
        for k, v in srdict.iteritems():
            body = body.replace(" %s " % k.upper(), " %s " % v.upper())
            body = body.replace(" %s " % k.lower(), " %s " % v.lower())
            body = body.replace(" %s " % k.title(), " %s " % v.title())
        # Restore escaped values to normal
        for word in ("my", "me"):
            body = body.replace(" ##%s## " % word.upper(), " %s " % word.upper()).replace(" ##%s## " % word.lower(), " %s " % word.lower()).replace(" ##%s## " % word.title(), " %s " % word.title())
        body = body.strip()
        return body

    def is_temporary(self):
        now = datetime.datetime.now()
        return (self.finish_time.year - now.year) <= 2

    def __unicode__(self):
        return self.verb.name + " " + self.body

class Friendship(db.Model):

    me = db.ReferenceProperty(Person, required=True, collection_name='friendships')
    them = db.ReferenceProperty(Person, required=True, collection_name='friend_of')
    notify_by_email = db.BooleanProperty(default=False)
    create_date = db.DateTimeProperty(required=True, auto_now_add=True)

    def __unicode__(self):
        return "Friendship between " + self.me + " and " + self.them
